package com.erhuogo.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.OneCategoryDao;
import com.erhuogo.dao.TwoCategoryDao;
import com.erhuogo.entity.OneCategory;
import com.erhuogo.entity.TwoCategory;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.OneCategoryService;
import com.erhuogo.service.TwoCategoryService;
import com.erhuogo.util.DateUtils;

/**
 * 一级分类Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 下午12:00:50
 */
public class OneCategoryServiceImpl implements OneCategoryService {
	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	// 日志
	private static Logger logger = LoggerFactory.getLogger(OneCategoryServiceImpl.class);
			
	// 注入OneCategoryDao
	private OneCategoryDao oneCategoryDao;

	public OneCategoryDao getOneCategoryDao() {
		return oneCategoryDao;
	}

	public void setOneCategoryDao(OneCategoryDao oneCategoryDao) {
		this.oneCategoryDao = oneCategoryDao;
	}
	
	// 注入TwoCategoryDao
	private TwoCategoryDao twoCategoryDao;

	public TwoCategoryDao getTwoCategoryDao() {
		return twoCategoryDao;
	}

	public void setTwoCategoryDao(TwoCategoryDao twoCategoryDao) {
		this.twoCategoryDao = twoCategoryDao;
	}
	
	// 注入TwoCategoryService
	private TwoCategoryService twoCategoryService;

	public TwoCategoryService getTwoCategoryService() {
		return twoCategoryService;
	}

	public void setTwoCategoryService(TwoCategoryService twoCategoryService) {
		this.twoCategoryService = twoCategoryService;
	}

	// 1.获取所有启用的一级商品分类信息Service实现类
	@Override
	public List<OneCategory> findAllOneCategory() {
		List<OneCategory> listOneCategory =  oneCategoryDao.findAllOneCategory();
		return listOneCategory;
	}

	// 2.分页条件查询一级分类信息Service实现类
	@Override
	public GridPageBean<OneCategory> findByPage(String pageNo, String pageSize, String id, String name, String code,
			String startDate, String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = oneCategoryDao.countByParams(id, name, code, startDate, endDate);
		GridPageBean<OneCategory> result = new GridPageBean<OneCategory>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<OneCategory> rows = oneCategoryDao.findPageByParams(id,name,code,startDate,endDate,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<OneCategory> rows = oneCategoryDao.findPageByParams(id,name,code,startDate,endDate,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 3.根据ID查询一级分类Service实现类
	@Override
	public OneCategory findById(String idStr) {
		OneCategory oneCategory = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			oneCategory = oneCategoryDao.findById(id);
		}
		return oneCategory;
	}

	// 4.保存或更新一级分类信息Service实现类
	@Override
	public void save(String idStr, String code, String name, String desc) throws Exception {
		OneCategory oneCategory = new OneCategory();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			oneCategory = oneCategoryDao.findById(id);
		}else{
			// id为空则是添加新的三级分类
			oneCategory.setCreateTime(DateUtils.DateToStr(new Date()));
			oneCategory.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(code)){
			oneCategory.setCode(code);
		}
		if(!StringUtils.isEmpty(name)){
			oneCategory.setName(name);
		}
		if(!StringUtils.isEmpty(desc)){
			oneCategory.setDesc(desc);
		}
		oneCategory.setAlterTime(DateUtils.DateToStr(new Date()));
		oneCategoryDao.saveOneCategory(oneCategory);
	}

	// 5.更新一级分类信息的启用状态Service实现类
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			oneCategoryDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
			updateAllTwoCategoryStatusByOneId(status,id);
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			oneCategoryDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
			updateAllTwoCategoryStatusByOneId(status,id);
		}
	}

	/**
	 * 6.根据一级分类ID更新所有的二级分类状态
	 * @param status
	 * @param id
	 * @throws Exception
	 */
	private void updateAllTwoCategoryStatusByOneId(Integer status, Integer id) throws Exception {
		List<TwoCategory> listTwoCategory = twoCategoryDao.findAllTwoCategoryByOneId(id);
		if(listTwoCategory!=null && listTwoCategory.size()>0){
			logger.info("该一级分类下查询到["+listTwoCategory.size()+"]个二级分类,正在同步"+getTransStatus(status));
			for (TwoCategory twoCategory : listTwoCategory) {
				twoCategoryService.updateStatus(twoCategory.getId().toString(), status.toString());
			}
		}else{
			logger.info("该一级分类下未查询到二级分类");
		}
	}
	
	/**
	 * 7.获取正在如何更新状态
	 * @param status
	 * @return
	 */
	public String getTransStatus(Integer status){
		String result = null;
		if(status!=null){
			if(status==0){
				result = "启用";
			}else{
				result = "屏蔽";
			}
		}
		return result;
	}
	
}
